 
import { JSONBase, JSONObject } from "../../ADCore/Common/JSONReturn";
import { MusicEnums } from "../Enums/MusicEnums";
import ADQ from "../../ADCore/Core/ADQ";
import { IProgressManager } from "../../ADCore/Interfaces/IGameManager";
import { DataTypeReader } from "../Enums/DataTypeReader";
import { DataType } from "../Enums/DataType";

/**游戏数据管理器 */
export default class GameDataManager {
  static progress: IProgressManager;
  /**加载初始化数据 */
  loadInitData(_progress?: IProgressManager) {
    GameDataManager.progress = _progress;
    if (!GameDataManager.progress) {
      GameDataManager.progress = ADQ.getProgressManager(null, 0);
    }
    this.loadMusic();
    this.loadAtlas();
    this.loadJsonData();
    this.loadBitmapFont();
  }

  loadBitmapFont() {
    GameDataManager.progress.max += 2;
    //加载默认字体文件
    var defaultFont: Laya.BitmapFont = new Laya.BitmapFont();
    defaultFont.loadFont(
      "sources/fonts/default.fnt",
      Laya.Handler.create(this, () => {
        Laya.Text.registerBitmapFont("default", defaultFont);
        GameDataManager.progress.add();
      })
    );

    var baiGe: Laya.BitmapFont = new Laya.BitmapFont();
    baiGe.loadFont(
      "sources/fonts/score_font.fnt",
      Laya.Handler.create(this, () => {
        Laya.Text.registerBitmapFont("score_font", baiGe);
        GameDataManager.progress.add();
      })
    );
  }
  /**加载系统数据 */
  loadJsonData() {
    var initDataArray = DataTypeReader.Instance.apiList.takeList(
      0,
      DataType.initDataIndex
    );
    GameDataManager.progress.max += initDataArray.count;
    for (let i = 0; i < initDataArray.count; i++) {
      let url = initDataArray.values[i];
      let types = initDataArray.keys[i];
      ADQ.Http.get(url).then((result: JSONObject) => {
        ADQ.getDataFactory().addArrayToSort(types, result.data);
        GameDataManager.progress.add();
      });
    }
  }
  /**加载用户数据 */
  loadUserData() {
    var initDataArray = DataTypeReader.Instance.apiList.takeList(
      DataType.initDataIndex + 1,
      DataType.userDataIndex
    );
    for (let i = 0; i < initDataArray.count; i++) {
      let url = initDataArray.values[i];
      let types = initDataArray.keys[i];
      ADQ.Http.get(url).then((result: JSONObject) => {
        ADQ.getDataFactory().addArrayToSort(types, result.data);
      });
    }
  }
  loadAtlas() {
    GameDataManager.progress.max += 2;
    Laya.loader.load(
      "res/atlas/comp.atlas",
      Laya.Handler.create(this, () => {
        GameDataManager.progress.add();
      }),
      null,
      Laya.Loader.ATLAS
    );
    var effect_001 = new Laya.Animation();
    effect_001.loadAtlas(
      "sources/effects/effect_001.atlas",
      Laya.Handler.create(this, () => {
        GameDataManager.progress.add();
      }),
      "effect_001"
    );
  }
  loadMusic() {
    GameDataManager.progress.max += 5;
    ADQ.globalSound.registerMusic(
      MusicEnums.backgroudMusic,
      "sources/musics/xxxx.mp3"
    );
    ADQ.globalSound.loadSoud((rs) => {
      GameDataManager.progress.add();
    }, this);
  }
  loadScene() {
    var sceneProgress = 20;
    GameDataManager.progress.max += sceneProgress;
    var loadResouce = "MainScenes/HomePage/loadResouce.scene";

    Laya.Scene.load(
      loadResouce,
      Laya.Handler.create(this, (scene) => {
        var sceneSprite = scene as Laya.Sprite;
        sceneSprite.destroy(true);
      }),
      Laya.Handler.create(
        this,
        (nums) => {
          GameDataManager.progress.setFixed(nums * sceneProgress);
        },
        null,
        false
      )
    );
  }
}
